package com.yyairmarkyy.arctool.util;

import android.content.Context;
import android.view.View;

/**
 * Created by aiyou on 2017/6/29.
 */
public class TouchUtil {


    /**
     * 非全屏下是否是都脱出屏幕
     *
     * @param context
     * @param x 当前view的x
     * @param y 当前view的y
     * @param view 需要判断的view
     * @return
     */
    public static int[] isOutOfScreenEage(Context context,int x, int y, View view) {
        int[] loaction = new int[2];
        loaction[0] = caluIsXOut(context,x,view);
        loaction[1] = cluIsYOut(context,y,getStatusBarHei(context),view);
        return loaction;
    }

    /**
     * 全屏下是否脱出屏幕
     * 此方法可以用在一个外层的view上也可以用在嵌套的view上
     * @param context
     * @param x
     * @param y
     * @param view
     * @return
     */
    public static int[] isOutOfScreenEageNoStatusBar(Context context,int x,int y,View view){
        int[] loaction = new int[2];
        loaction[0] = caluIsXOut(context,x,view);
        loaction[1] = cluIsYOut(context,y,0,view);
        return loaction;
    }

    private static int caluIsXOut(Context context,int x,View view){
        int resultX;
        if (x < -view.getLeft()) {
            resultX = -view.getLeft();
        } else if (x > getScreenWidth(context) - view.getWidth()-view.getLeft()) {
            resultX = getScreenWidth(context) - view.getWidth()-view.getLeft();
        } else {
            resultX = x;
        }
        return resultX;
    }

    private static int cluIsYOut(Context context,int y,int statusBarHeight,View view){
        int resultY;
        if (y < -view.getTop()) {
            resultY = -view.getTop();
        } else if (y > getScreenHeight(context) - view.getHeight() - view.getTop() - statusBarHeight) {
            resultY = getScreenHeight(context) - view.getHeight() - view.getTop() - statusBarHeight;

        } else {
            resultY = y;
        }

        return resultY;
    }

    private static int getStatusBarHei(Context context)
    {
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = context.getResources().getDimensionPixelSize(resourceId);
        }
        //Log.i("WangJ", "status bar height:" + statusBarHeight1);
        return statusBarHeight1;
    }



    /**
     * 获取屏幕宽度
     *
     * @param ctx
     * @return
     */
    private static int getScreenWidth(Context ctx) {
        return ctx.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 获取屏幕高度
     *
     * @param ctx
     * @return
     */
    private static int getScreenHeight(Context ctx) {
        return ctx.getResources().getDisplayMetrics().heightPixels;
    }


}